<%--
  Created by IntelliJ IDEA.
  User: dqf
  Date: 2015/3/4
  Time: 16:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<div class="row createBtn">
    <div class="col-sm-12 col-md-12 col-lg-12">
        <div >
            <shiro:hasAnyRoles name="wechat">
            <form id="searchForm" method="post" action="">
                <a id="ajax_linkman_btn_add" class="btn btn-default " data-toggle="modal"><i class="fa fa-lg fa-plus"></i> 新建联系人</a>
                <a id="ajax_download_proInfo_datas" class="btn btn-default pull-right" href="javascript:void(0);"> <i class="fa fa-lg fa-hand-o-right"></i> 进入下载中心 </a>
                <a id="ajax_export_proInfo_datas" class="btn btn-default pull-right" href="javascript:void(0);" style="margin-right: 5px;"><i class="fa fa-lg fa-external-link"></i> 联系人信息数据导出 </a>
            </form>
            </shiro:hasAnyRoles>
        </div>
    </div>
</div>
<input type="hidden" id="selectedIds" value=""/>
<section id="widget-grid" class="">

    <!-- row -->
    <div class="row">
        <!-- NEW WIDGET START -->
        <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12">

            <!-- Widget ID (each widget will need unique ID)-->
            <div class="jarviswidget " id="wid-id-0" data-widget-editbutton="false"
                 data-widget-colorbutton="false"
                 data-widget-togglebutton="false"
                 data-widget-deletebutton="false"
                 data-widget-fullscreenbutton="true"
                 data-widget-custombutton="false"
                 data-widget-sortable="false">

                <%--<header>--%>
                <%--<span class="widget-icon"> <i class="fa fa-table"></i> </span>--%>
                <%--<h2>客户信息列表</h2>--%>
                <%--</header>--%>

                <!-- widget div-->
                <div>

                    <!-- widget edit box -->
                    <div class="jarviswidget-editbox">
                        <!-- This area used as dropdown edit box -->

                    </div>
                    <!-- end widget edit box -->

                    <!-- widget content -->
                    <div class="widget-body no-padding">

                        <div class="col-sm-12 col-md-12 col-lg-12">
                            <div >
                                <div class="row" id="ajax_linkman_list_row">
                                    <table id="ajax_linkman_table" class="table table-striped table-bordered table-hover">
                                    </table>
                                    <div id="ajax_linkman_list_page">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- end widget content -->

                </div>
                <!-- end widget div -->

            </div>
            <!-- end widget -->

        </article>
        <!-- WIDGET END -->

    </div>

    <!-- end row -->

    <!-- end row -->

</section>


<!-- end widget grid -->
<script>
    $("#ajax_linkman_btn_add").click(function(){
        loadURL("ajax-linkman!input.action",$('#business_data'));
    });
</script>

<script type="text/javascript">
    pageSetUp();
    $(function(){
        $("#selectedIds").val("");
        load_client_jqGrid();
    });
    function load_client_jqGrid(){
        jQuery("#ajax_linkman_table").jqGrid({
            url:'ajax-linkman!list.action?clientId='+$('#keyId').val(),
            datatype: "json",
            colNames:['客户名称',"姓名",'常用称呼',"职位","办公电话","手机","邮箱","生日","操作","id"],
            colModel:[
                {name:'client_name',index:'client', width:80,sortable:true,search:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'name',index:'name', width:60,search:true,sortable:true,
                    searchoptions:{sopt: ["cn"],clearSearch:true}},
                {name:'subname',index:'subName', search:false,width:60},
                {name:'duty',index:'duty', search:false,width:120},
                {name:'tel',index:'tel', search:false,width:100},
                {name:'mobile',index:'mobile',search:false,width:100},
                {name:'email',index:'email',search:false,width:100},
                {name:'birthday',index:'birthday',search:false,width:100},
                {name:'act',index:'act', search:false,width:210,sortable:false,fixed:true},
                {name:'id',index:'id',hidden:true,search:false},
            ],
            rowNum : 10,
            rowList:[10,20,30],
            pager : '#ajax_linkman_list_page',
            sortname : '',
            sortorder : "",
            gridComplete:function(){

                var ids=$("#ajax_linkman_table").jqGrid('getDataIDs');
                var selectedIds = $("#selectedIds").val();
                for(var i=0;i<ids.length;i++){
                    var cl=ids[i];
                    if (selectedIds != "" && selectedIds != null){
                        var idsArr = selectedIds.split(",");
                        for (var s=0;s<idsArr.length;s++){
                            if (idsArr[s] == cl){
                                $("#ajax_linkman_table").jqGrid("setSelection", cl);
                            }
                            // break;
                        }
                    }
                    var rowData = $("#ajax_linkman_table").jqGrid("getRowData",cl);
                    var de="<button class='btn btn-default' data-original-title='查看' onclick=\"fn_linkman_read('"+cl+"');\"><i class='fa fa-eye'></i>查看</button>"+" ";
                    var dt="<button class='btn btn-default' data-original-title='删除' onclick=\"fn_linkman_destroy('"+cl+"');\"><i class='fa fa-eye'></i>删除</button>"+" ";
                    if($("#ajax_linkman_btn_add").length==0){
                        dt="";
                    }
                    jQuery("#ajax_linkman_table").jqGrid('setRowData',ids[i],{act:de+dt});
                }

                $(".ui-jqgrid-bdiv").css("overflow-x","hidden");

                jqGridStyle();

            },
            onSelectRow: function (rowId, status, e) {
                var ids = $("#selectedIds").val();
                if (ids != ""){
                    var idsArr = ids.split(",");
                    var isSelected = false;
                    for (var i=0; i<idsArr.length;i++){
                        if (idsArr[i] == rowId){
                            isSelected = true;
                            break;
                        }
                    }
                    if (status){
                        if (!isSelected)
                            $("#selectedIds").val(ids + "," +rowId);
                    }
                    else{
                        if (isSelected) {
                            if (idsArr.length < 2){
                                $("#selectedIds").val("");
                            }
                            else{
                                for (var i = 0; i < idsArr.length; i++) {
                                    if (idsArr[i] == rowId) {
                                        for (var j = i; j < idsArr.length - 1; j++) {
                                            idsArr[j] = idsArr[j + 1];
                                        }
                                        idsArr.length = idsArr.length - 1;
                                        break;
                                    }
                                }
                                $("#selectedIds").val(idsArr.join(","));
                            }
                        }
                    }
                }
                else{
                    $("#selectedIds").val(rowId);
                }

            },
            onSelectAll:function(aRowids, status, e){
                var ids = $("#selectedIds").val();
                if (ids != "") {
                    var idsArr = ids.split(",");
                    for (var i = 0; i < aRowids.length; i++) {
                        var isSelected = false;
                        var index = -1;
                        for (var j = 0; j < idsArr.length; j++) {
                            if (aRowids[i] == idsArr[j]) {
                                isSelected = true;
                                index = j;
                                break;
                            }
                        }
                        if (status) {
                            if (!isSelected) {
                                idsArr.push(aRowids[i]);
                            }
                        }
                        else {
                            if (isSelected) {
                                for (var n = index; n < idsArr.length - 1; n++) {
                                    idsArr[n] = idsArr[n + 1];
                                }
                                idsArr.length = idsArr.length - 1;
                            }
                        }
                    }
                    $("#selectedIds").val(idsArr.join(","));
                }
                else{
                    $("#selectedIds").val(aRowids.join(","));
                }
            },
            jsonReader: {
                root: "dataRows",
                page: "page",
                total: "total",
                records: "records",
                repeatitems : false
            },
            caption : "<i class='fa fa-arrow-circle-right'></i> 联系人列表",
            multiselect : true,
            rownumbers:true,
            gridview:true,
            shrinkToFit:true,
            viewrecords: true,
            autowidth: true,
            height:'auto',
            forceFit:true,
            loadComplete: function() {
            }
        });
        $(window).on('resize.jqGrid', function() {
            jQuery("#ajax_linkman_table").jqGrid('setGridWidth', $("#ajax_linkman_list_row").width());
        });
        jQuery("#ajax_linkman_table").jqGrid('filterToolbar',{searchOperators:false,stringResult:true});
        jQuery("#ajax_linkman_table").jqGrid('navGrid', "#ajax_linkman_list_page", {
            edit : false,
            add : false,
            del : false,
            search:false
        });
    }

    function fn_linkman_read(id){
        loadURL("ajax-linkman!read.action?keyId="+id,$('#business_data'));
    }

    function fn_linkman_destroy(id){
        var vActionUrl = "<%=path%>/com/ajax-linkman!delete.action";
        data={keyId:id};
        ajax_action(vActionUrl,data,{},function(pdata){
            _show(pdata);
        });
        jQuery("#ajax_linkman_table").trigger("reloadGrid");
    }
    $("#ajax_export_proInfo_datas").click(function(){
      // alert($("#selectedIds").val());
        if($("#selectedIds").val()==""){
            var data = {
                state:400,
                title:"操作状态",
                message:"请先勾选需要导出的联系人信息"
            }
            _show(data);
            return;
        }
        $.SmartMessageBox({
            title : "确定导出联系人信息数据吗？",
            content : "确定导出操作将进入创建导出文件队列，进入下载中心可查看导出文件执行情况。",
            buttons : '[取消][确认]'
        }, function(ButtonPressed) {
            if (ButtonPressed === "确认") {
                var idList=$("input#selectedIds").val();
                location.href = "ajax-import!exportData.action?keyId=linkman&idList="+idList;
                /*
                ajax_action("ajax-import!exportData.action?keyId=linkman"+"&idList="+idList,null,{},function(pdata){
                    _show(pdata);
                });*/
            }
        });

    });

    $("#ajax_download_proInfo_datas").click(function(){
        loadURL("ajax!downloadFile.action",$("#content"));
    });
</script>